#include<bits/stdc++.h>

using namespace std;

int read_int() {
    int x;
    cin >> x;
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    auto q = read_int();
    deque<int> v;
    while (q--) {
        int op = read_int();
        if (op == 0) {
            int d = read_int(), x = read_int();
            if (d == 1)v.push_back(x);
            else if (d == 0)v.push_front(x);
        } else if (op == 1) {
            cout << v[read_int()] << endl;
        } else {
            int d = read_int();
            if (d == 1)v.pop_back();
            else v.pop_front();
        }
    }

    return 0;
}